﻿@* @inject IDialogService DialogService
@inject IProductionSchedulingService ProductionSchedulingService
@inject IStandardBomService StandardBomService

<MudStack>
    <MudToolBar>
        <MudSpacer></MudSpacer>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle"
                   Size="Size.Small" Class="mr-5">
            保存
        </MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle"
                   Size="Size.Small">
            取消
        </MudButton>
    </MudToolBar>
    <MudPaper Class="pa-4" Elevation="0">
        <MudText Color="Color.Primary" Typo="Typo.h6">基本信息</MudText>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto" Validation="@(_editValidator.ValidateValue)" ValidationDelay="0">
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Code" For="(() => _editRequestDto.Code)"
                                  Variant="Variant.Outlined" Margin="Margin.Dense"
                                  Label="生产排产编码">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Name" For="(() => _editRequestDto.Name)"
                                  Variant="Variant.Outlined" Margin="Margin.Dense"
                                  Label="生产排产名称">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="(() => _editRequestDto.Description)"
                                  Variant="Variant.Outlined" Margin="Margin.Dense"
                                  Label="描述" Lines="3">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </MudPaper>
    <MudPaper Class="pa-4" Elevation="0">
        <MudText Color="Color.Primary" Typo="Typo.h6">工单信息</MudText>
        <MudGrid>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder!.Code" Label="工单编码"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder!.Name" Label="工单名称"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.StandardBom!.Code" Label="BOM编码"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.StandardBom!.Name" Label="BOM名称"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder.Quantity" Label="工单数量"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder.ExpectedTime" Label="预期时间"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder.EndTime" Label="截止时间"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder.PlanStartTime" Label="计划开始时间"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
            <MudItem xs="6">
                <MudTextField @bind-Value="_editRequestDto.ProductionOrder.PlanEndTime" Label="计划结束时间"
                              Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                </MudTextField>
            </MudItem>
        </MudGrid>
    </MudPaper>
    <MudPaper Class="pa-4" Elevation="0">
        <MudText Color="Color.Primary" Typo="Typo.h6">明细信息</MudText>
        <MudTable T="ProductionSchedulingDetailEditRequestDto" Items="_editRequestDto.Items"
                  Hover="true" Dense="true" Breakpoint="Breakpoint.Xs" Elevation="0"
                  ReadOnly="true" FixedHeader="true" CustomFooter="true">
            <ToolBarContent>
                <MudButton StartIcon="mdi mdi-plus" Variant="Variant.Filled" Color="Color.Primary"
                           Size="Size.Small" Class="app-default-button" OnClick="AddHandle">
                    新增
                </MudButton>
            </ToolBarContent>
            <Columns>
                <MudColumn HeaderText="工序编号" Value="@(context.ProcessCode)"></MudColumn>
                <MudColumn HeaderText="工序名称" Value="@(context.ProcessName)"></MudColumn>
                <MudTemplateColumn T="ProductionSchedulingDetailEditRequestDto">
                    <Header Context="_">
                        <MudTh>操作</MudTh>
                    </Header>
                    <Row Context="_">
                        <MudTd>
                            <MudButton StartIcon="@Icons.Material.Filled.Edit" Size="Size.Small" OnClick="@(() => EditHandle(context))">修改</MudButton>
                            <MudButton StartIcon="@Icons.Material.Filled.Delete" Size="Size.Small" OnClick="DeleteHandler">删除</MudButton>
                        </MudTd>
                    </Row>
                </MudTemplateColumn>
                <MudColumn HeaderText="下一道工序" Value="@("ProcessName")"></MudColumn>
                <MudColumn HeaderText="与下一道工序关系" Value="@("ProcessName")"></MudColumn>
            </Columns>
        </MudTable>
    </MudPaper>
</MudStack>

@code {
    private int _initKey = -1;
    private ProductionSchedulingEditRequestDto _editRequestDto = new();
    private ProductionSchedulingEditValidator _editValidator = new();

    private MudForm _editRequestForm = new();

    private DialogOptions _dialogOptions = new()
    {
        MaxWidth = MaxWidth.Medium,
        CloseButton = true,
        FullWidth = true,
        DisableBackdropClick = true
    };

    [CascadingParameter]
    public ProductionSchedulingIndex? ProductionSchedulingIndex { get; set; }


    async Task AddHandle()
    {
        ProductionSchedulingDetailEditRequestDto itemData = new()
        {
            Key = _initKey
        };
        var dialog = await DialogService.ShowAsync<ProductionSchedulingDetailEdit>("添加工序", new DialogParameters()
        {
            { "Model", itemData }
        }, _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            _initKey--;
            itemData = (ProductionSchedulingDetailEditRequestDto)result.Data;
            _editRequestDto.Items.Add(itemData);
        }
    }

    async Task DeleteHandler()
    {
    }

    async Task EditHandle(ProductionSchedulingDetailEditRequestDto itemData)
    {
        var dialog = await DialogService.ShowAsync<ProductionSchedulingDetailEdit>("修改工序", new DialogParameters()
        {
            { "EditRequestDto", itemData }
        }, _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            itemData = (ProductionSchedulingDetailEditRequestDto)result.Data;
        }
    }

    private async Task<IEnumerable<StandardBomInfoResponseDto>> StandardBomSearch(string value)
    {
        var result = await StandardBomService.QueryStandardBoms(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            return result.Data!;
        }
        return new List<StandardBomInfoResponseDto>();
    }

    void CancelHandle()
    {
        ProductionSchedulingIndex?.SetDynamicComponentType(typeof(ProductionSchedulingTable));
    }

    async Task SubmitHandle()
    {
        await _editRequestForm.Validate();

        var key = ProductionSchedulingIndex?.GetKey();

        // if (key > 0)
        // {
        //     var result = await ProductionSchedulingService.UpdateProductionScheduling(key.Value, _editRequestDto);
        // }
        // else
        // {
        //     var result = await ProductionSchedulingService.CreateProductionScheduling(_editRequestDto);
        // }
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new ProductionSchedulingEditRequestDto();
        // var key = ProductionSchedulingIndex?.GetKey();
        // if (key > 0)
        // {
        //     var result = await ProductionSchedulingService.GetProductionScheduling(key.Value);
        //     if (result != null && result.Succeeded && result.Data != null)
        //     {
        //         _editRequestDto.Code = result.Data.Code;
        //         _editRequestDto.Name = result.Data.Name;
        //         _editRequestDto.StandardBom.Id = result.Data.StandardBomId;
        //         _editRequestDto.StandardBom.Code = result.Data.StandardBomCode;
        //         _editRequestDto.StandardBom.Name = result.Data.StandardBomName;
        //         _editRequestDto.Description = result.Data.Description;
        //         if (result.Data.Items != null)
        //         {
        //             _editRequestDto.Items = result.Data.Items.Select(i => new ProductionSchedulingDetailEditRequestDto()
        //             {
        //                 Key = i.Id,
        //                 Process = new ProcessInfoResponseDto()
        //                 {
        //                     Id = i.Id,
        //                     Code = i.ProcessCode,
        //                     Name = i.ProcessName
        //                 },
        //                 Sort = i.Sort,
        //                 TakeTime = i.TakeTime
        //             }).ToList();
        //         }
        //     }
        // }

        await base.OnParametersSetAsync();
    }

} *@